package com.qinggeng.project.system.service.impl;

import com.qinggeng.common.enums.ShowTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.project.api.dto.HelpRecordDto;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.system.domain.UserBeat;
import com.qinggeng.project.system.domain.UserBeatHelp;
import com.qinggeng.project.system.mapper.UserBeatHelpMapper;
import com.qinggeng.project.system.service.UserBeatHelpService;
import org.apache.poi.xssf.streaming.SheetDataWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author 吴鹏飞
 * @date 2020/11/5
 * 用户帮捶记录 业务实现
 */
@Service
public class UserBeatHelpServiceImpl implements UserBeatHelpService {
    @Resource
    private UserBeatHelpMapper userBeatHelpMapper;
    /**
     * 插入帮捶记录
     * @param helpRecordDto
     */
    @Override
    public void insertUserBeatHelp(HelpRecordDto helpRecordDto) {
        userBeatHelpMapper.insertUserBeatHelp(helpRecordDto);
    }
    /**
     * 根据用户id查询当天所捶的商品id集合
     * @param userId
     * @return
     */
    @Override
    public List<UserBeatHelp> selectUserBeatHelpByUserIdAndNow(Long userId) {
        return userBeatHelpMapper.selectUserBeatHelpByUserIdAndNow(userId);
    }

    @Override
    public String getUserBeatPrice(List<String> random, UserBeat userBeat) {

        if(null==userBeat){
            throw new CustomException("此捶一捶订单不存在", ShowTypeEnum.用户.getValue());
        }
        if(null==userBeat.getUserId()){
            throw new CustomException("捶一捶订单发起人不存在", ShowTypeEnum.用户.getValue());
        }
        if(random.size()<=0){
            throw new CustomException("此订单已捶成",ShowTypeEnum.用户.getValue());
        }
        //帮捶自己 或 用户已帮别人捶过
        if(userBeat.getUserId().compareTo(ApiUtils.getUserId())==0 ||!userBeatHelpMapper.checkUserBeatHelpIsFirst(ApiUtils.getUserId())){
            return random.remove(random.size()-1);
        }
        BigDecimal beatPrice=new BigDecimal(0);
        for (int i=1;i<=2;i++) {
            beatPrice=beatPrice.add(new BigDecimal(random.remove(random.size()-1)));
            if(random.size()==0){
                break;
            }
        }
        return beatPrice.toString();
    }

}
